package com.telecom.weixi.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.telecom.weixi.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DataSearchAdapter extends BaseAdapter {
	List<Map<String, Object>> mTypeData;
	LayoutInflater mFlater;
	
	public static final String Tag_Main_Title = "main_title";		//String
	public static final String Tag_Sub_Title  = "sub_title";		//String
	public static final String Tag_Type_Logo = "type_logo";			//drawable
	
	public DataSearchAdapter(Context context){
		mFlater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTypeData = new ArrayList<Map<String, Object>>();
	}
	
	public void addItem(Map<String, Object> item){
		mTypeData.add(item);
	}
	
	public void clearData(){
		mTypeData.clear();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mTypeData.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return mTypeData.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(convertView != null){
			holder = (ViewHolder) convertView.getTag();
		}else{
			holder = new ViewHolder();
			convertView = mFlater.inflate(R.layout.item_data_search, null); 
			holder.title = (TextView) convertView.findViewById(R.id.main_title);
			holder.subTitle = (TextView) convertView.findViewById(R.id.sub_title);
			holder.logo = (ImageView) convertView.findViewById(R.id.type_logo);
			
			convertView.setTag(holder);
		}
		
		Map<String, Object> item = mTypeData.get(pos);
		holder.title.setText((CharSequence) item.get(Tag_Main_Title));
		holder.subTitle.setText((CharSequence) item.get(Tag_Sub_Title));
		Drawable icon = (Drawable) item.get(Tag_Type_Logo);
		if(icon != null ){
			holder.logo.setImageDrawable(icon);
		}
		return convertView;
	}
	
	class ViewHolder{
		ImageView logo;
		TextView title;
		TextView subTitle;
	}

}
